package com.datatools.ui.sqledit;

import java.util.StringTokenizer;

/**
 * 增强 StringTokenizer
 **/
public class SQLTokenizer extends StringTokenizer {

    public SQLTokenizer(String str) {
        super(str, " ");
        this.oldStr = str;
        this.str = str;
    }

    //字符串默认分隔符 " "英文半角空格
    private String delimit = " ";
    private String oldStr;
    private String str;
    //当前位置
    private int currPosition = 0;
    //开始位置
    private int beginPosition = 0;

    public int getCurrPosition() {
        return currPosition;
    }

    @Override
    public String nextToken() {
        try {
            String s = super.nextToken();
            if (oldStr.equals(s)) {
                return oldStr;
            }
            int pos = -1;
            pos = str.indexOf(s + delimit);
            if (pos == -1) {
                pos = str.indexOf(delimit + s);
                if (pos == -1) {
                    return null;
                } else {
                    pos += 1;
                }
            }
            int xBegin = pos + s.length();
            str = str.substring(xBegin);
            currPosition = beginPosition + pos;
            beginPosition = beginPosition + xBegin;
            return s;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }


}
